(function(l) {
    function k(c) {
        var b = [Math.round(c.r).toString(16), Math.round(c.g).toString(16), Math.round(c.b).toString(16)];
        a.each(b, function(c, a) {
            if (a.length == 1) {
                b[c] = '0' + a;
            }
        });
        return '#' + b.join('');
    }

    function i(a) {
        a = parseInt(a.indexOf('#') > -1 ? a.substring(1) : a, 16);
        return {
            r: a >> 16,
            g: (a & 0x00FF00) >> 8,
            b: a & 0x0000FF
        };
    }

    function d(j) {
        var d, e, f, c = j.h,
            i = j.s * 255 / 100,
            h = j.v * 255 / 100;
        if (i === 0) {
            d = e = f = h;
        } else {
            var b = h,
                a = (255 - i) * h / 255,
                g = (b - a) * (c % 60) / 60;
            if (c == 360) {
                c = 0;
            }
            if (c < 60) {
                d = b;
                f = a;
                e = a + g;
            } else if (c < 120) {
                e = b;
                f = a;
                d = b - g;
            } else if (c < 180) {
                e = b;
                d = a;
                f = a + g;
            } else if (c < 240) {
                f = b;
                d = a;
                e = b - g;
            } else if (c < 300) {
                f = b;
                e = a;
                d = a + g;
            } else if (c < 360) {
                d = b;
                e = a;
                f = b - g;
            } else {
                d = e = f = 0;
            }
        }
        return {
            r: d,
            g: e,
            b: f
        };
    }

    function e(a) {
        var b = 0,
            e, f, g = Math.min(a.r, a.g, a.b),
            c = Math.max(a.r, a.g, a.b),
            d = c - g;
        f = c;
        e = c ? 255 * d / c : 0;
        if (e) {
            if (a.r == c) {
                b = (a.g - a.b) / d;
            } else if (a.g == c) {
                b = 2 + (a.b - a.r) / d;
            } else {
                b = 4 + (a.r - a.g) / d;
            }
        } else {
            b = -1;
        }
        b *= 60;
        if (b < 0) {
            b += 360;
        }
        e *= 100 / 255;
        f *= 100 / 255;
        return {
            h: b,
            s: e,
            v: f
        };
    }

    function b(a) {
        return k(d(a));
    }

    function h(a) {
        return e(i(a));
    }
    var c = mobiscroll,
        a = c.$,
        f = c.util.prefix,
        g = c.presets.scroller,
        j = {
            preview: true,
            previewText: true,
            label: 'Color',
            refineLabel: 'Refine',
            step: 10,
            nr: 10,
            format: 'hex',
            hueText: 'Hue',
            saturationText: 'Saturation',
            valueText: 'Value'
        };
    c.presetShort('color');
    g.color = function(i) {
        function y(b, a) {
            return Math.floor(b / a) * a;
        }

        function g(a) {
            return isNaN(+a) ? 0 : +a;
        }

        function u(a) {
            if (k == 'hsv') {
                return a.join(',');
            } else if (k == 'rgb') {
                var c = d({
                    h: a[0],
                    s: a[1],
                    v: a[2]
                });
                return Math.round(c.r) + ',' + Math.round(c.g) + ',' + Math.round(c.b);
            }
            return b({
                h: a[0],
                s: a[1],
                v: a[2]
            });
        }

        function v(c, a, b) {
            c[0].style.backgroundImage = f + (f == '-webkit-' ? 'gradient(linear,left top,left bottom,from(' + a + '),to(' + b + '))' : 'linear-gradient(' + a + ',' + b + ')');
        }

        function n(f, g) {
            var c = i._tempWheelArray;
            if (g !== 1 && g !== 2) {
                v(a('.mbsc-sc-whl-sc', f).eq(1), b({
                    h: c[0],
                    s: 0,
                    v: 100
                }), b({
                    h: c[0],
                    s: 100,
                    v: 100
                }));
            }
            if (g !== 2) {
                v(a('.mbsc-sc-whl-sc', f).eq(2), b({
                    h: c[0],
                    s: c[1],
                    v: 0
                }), b({
                    h: c[0],
                    s: c[1],
                    v: 100
                }));
            }
            if (l) {
                var e = d({
                        h: c[0],
                        s: c[1],
                        v: c[2]
                    }),
                    h = e.r * 0.299 + e.g * 0.587 + e.b * 0.114;
                a('.mbsc-color-preview', f).attr('style', 'background:' + b({
                    h: c[0],
                    s: c[1],
                    v: c[2]
                }) + ';color:' + (h > 130 ? '#000' : '#fff')).text(w ? u(c) : '');
            }
        }

        function x() {
            var a = 0,
                c = {
                    data: [],
                    label: o
                },
                d = {
                    circular: false,
                    data: [],
                    label: q
                },
                e = {
                    circular: false,
                    data: [],
                    label: r
                };
            for (; a < 360; a += 3) {
                c.data.push({
                    value: a,
                    label: a,
                    display: '<div class="mbsc-color-itm" style="background:' + b({
                        h: a,
                        s: 100,
                        v: 100
                    }) + '"><div class="mbsc-color-itm-a"></div></div>'
                });
            }
            for (a = 0; a < 101; a += 1) {
                d.data.push({
                    value: a,
                    label: a,
                    display: '<div class="mbsc-color-itm"><div class="mbsc-color-itm-a"></div></div>'
                });
                e.data.push({
                    value: a,
                    label: a,
                    display: '<div class="mbsc-color-itm"><div class="mbsc-color-itm-a"></div></div>'
                });
            }
            return [
                [c, d, e]
            ];
        }
        var p, m = a.extend({}, i.settings),
            c = a.extend(i.settings, j, m),
            s = a.isArray(c.colors) ? c.colors : [c.colors],
            t = c.defaultValue || s[0],
            k = c.format,
            l = c.preview,
            w = c.previewText,
            o = c.hueText,
            q = c.saturationText,
            r = c.valueText;
        p = x();
        return {
            minWidth: 70,
            height: 15,
            rows: 13,
            speedUnit: 0.006,
            timeUnit: 0.05,
            showLabel: true,
            scroll3d: false,
            wheels: p,
            compClass: 'mbsc-color',
            parseValue: function(a) {
                var c, b;
                a = a || t;
                if (a) {
                    if (k == 'hsv') {
                        a = a.split(',');
                        b = {
                            h: g(a[0]),
                            s: g(a[1]),
                            v: g(a[2])
                        };
                    } else if (k == 'rgb') {
                        c = a.split(',');
                        b = e({
                            r: g(c[0]),
                            g: g(c[1]),
                            b: g(c[2])
                        });
                    } else {
                        a = a.replace('#', '');
                        if (a.length == 3) {
                            a = a[0] + a[0] + a[1] + a[1] + a[2] + a[2];
                        }
                        b = h(a);
                    }
                    return [y(Math.round(b.h), 3), Math.round(b.s), Math.round(b.v)];
                }
                return [0, 100, 100];
            },
            formatValue: u,
            onBeforeShow: function() {
                if (l) {
                    c.headerText = false;
                }
            },
            onMarkupReady: function(c) {
                var b = a(c.target);
                if (l) {
                    b.find('.mbsc-sc-whl-gr-c').before('<div class="mbsc-color-preview"></div>');
                }
                n(b);
            },
            validate: function(a) {
                if (i._isVisible) {
                    n(i._markup, a.index);
                }
            }
        };
    };
    c.util.color = {
        hsv2hex: b,
        hsv2rgb: d,
        rgb2hsv: e,
        rgb2hex: k,
        hex2rgb: i,
        hex2hsv: h
    };
}());